#!/bin/sh

TRACK_FILE=${HOME}/.local/share/${USER}/.track

[ $# -lt 1 ] && echo "usage: track TASK [HH:MM]" && exit 1

HHMM=
HHMM_FORMAT=$2
# handle gracefully the case that a specific hour and minute are specified
[ -n "$HHMM_FORMAT" ] && HHMM=" -d $2"
[ -z "$HHMM_FORMAT" ] && HHMM_FORMAT="%H:%M"

date +"%s $1 %Y-%m-%d $HHMM_FORMAT"$HHMM >> ${TRACK_FILE}

# in case of specified hh:mm, the line might need to be inserted at the right position
# this is more efficiently done by just inserting at the right place, but instead
# the track file can be sorted to deal with this issue
[ -n "$2" ] && sort -o "$TRACK_FILE" "$TRACK_FILE"
